package adke.gui.panels;

import adke.gui.Overview;
import adke.managers.PlayerManager;
import adke.models.Player;
import info.clearthought.layout.TableLayout;
import java.awt.Color;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

public class ScoreHolderPanel extends JPanel
{
    private Vector<ScorePanel> scorePanels;
    private TableLayout layout;
    
    private double scorePanelHeight = TableLayout.PREFERRED;
    
    public ScoreHolderPanel()
    {
        scorePanels = new Vector();
        setBackground(Color.BLACK);
        setBorder(BorderFactory.createLineBorder(Color.ORANGE));
        
        init();
    }
    
    public static ScoreHolderPanel getInstance()
    {
        return Overview.getInstance().getGamePanel().getScoreHolderPanel();
    }
    
    private void init()
    {
        refresh();
        
        // voeg panelen toe
        removeAll();
        
        double[][] size = { {TableLayout.FILL, TableLayout.PREFERRED, TableLayout.FILL}, 
                            {TableLayout.FILL, TableLayout.FILL} };
        layout = new TableLayout(size);
        setLayout(layout);
        
        for (ScorePanel panel : scorePanels)
        {
            layout.insertRow(1, scorePanelHeight);
            layout.insertRow(2, 5);
            add(panel, "1, 1");
        }
    }
    
    // update scores
    public void refresh()
    {
        Player[] players = PlayerManager.getPlayers();
        
        if (scorePanels.isEmpty())
        {
            scorePanels.clear();
            for (Player p : players)
            {
                scorePanels.add(new ScorePanel(p));
            }
        }
        else
        {
            for (ScorePanel panel: scorePanels)
            {
                panel.update();
            }
        }
    }
}
